package com.example.administrator.myapplication;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.myapplication.Bean.LoginBean;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;

public class LoginActivity extends AppCompatActivity {
    EditText EtUserName,EtPassword;
    Button btLogin;
    TextView TvRegister;
    String username,password;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        EtUserName =(EditText) findViewById(R.id.et_username);
        EtPassword = (EditText)findViewById(R.id.et_password);
        btLogin =(Button) findViewById(R.id.bt_login);
        TvRegister = (TextView) findViewById(R.id.tv_register);
        TvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }

    private void initData() {
        username = EtUserName.getText().toString() +"";
        password = EtPassword.getText().toString() +"";
        JSONObject jsonObject = new JSONObject();
        //将获取贷用户名和密码通过json形式传给服务端
        try {
            jsonObject.put("name",username);//用户名
            jsonObject.put("password",password);//密码
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //创建网络请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(LoginActivity.this);

        //发起网络请求
        String url = "http://www.xhjjyst.com/freshmenapp/user/login";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url,
                jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                Gson gson = new Gson();
                final LoginBean loginBean = gson.fromJson(jsonObject.toString(),LoginBean.class);
                final boolean success = loginBean.isSuccess();

                        if (success) {
                            // startActivity(new Intent(UserLoginActivity.this,HomeActivity.class));
                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                            startActivity(intent);
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(LoginActivity.this, "手机号码或密码错误", Toast.LENGTH_SHORT).show();
                        }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this,"请求失败",Toast.LENGTH_LONG).show();
            }
        });

        //讲请求内容嵌入队列中
        requestQueue.add(jsonObjectRequest);

    }
}